home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / hurd / hurdauth.c < prev    next >
C/C++ Source or Header  |  1994-05-11  |  3KB  |  130 lines

  1. /* Copyright (C) 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <hurd.h>
  20. #include <hurd/msg_server.h>
  21. #include <hurd/id.h>
  22.  
  23. int
  24. _hurd_refport_secure_p (mach_port_t ref)
  25. {
  26.   if (ref == __mach_task_self ())
  27.     return 1;
  28.   if (__USEPORT (AUTH, ref == port))
  29.     return 1;
  30.   return 0;
  31. }
  32.  
  33. kern_return_t
  34. _S_add_auth (mach_port_t me,
  35.          auth_t addauth)
  36. {
  37.   error_t err;
  38.   auth_t newauth;
  39.  
  40.   if (err = __USEPORT (AUTH,
  41.                __auth_makeauth (port,
  42.                     &addauth, 1, MACH_MSG_TYPE_MOVE_SEND,
  43.                     NULL, 0,
  44.                     NULL, 0,
  45.                     NULL, 0,
  46.                     NULL, 0,
  47.                     &newauth)))
  48.     return err;
  49.  
  50.   err = __setauth (newauth);
  51.   __mach_port_deallocate (__mach_task_self (), newauth);
  52.   if (err)
  53.     return errno;
  54.  
  55.   return 0;
  56. }
  57.  
  58. kern_return_t
  59. _S_del_auth (mach_port_t me,
  60.          task_t task,
  61.          intarray_t uids, mach_msg_type_number_t nuids,
  62.          intarray_t gids, mach_msg_type_number_t ngids)
  63. {
  64.   error_t err;
  65.   auth_t newauth;
  66.  
  67.   if (!_hurd_refport_secure_p (task))
  68.     return EPERM;
  69.  
  70.   HURD_CRITICAL_BEGIN;
  71.   __mutex_lock (&_hurd_id.lock);
  72.   err = _hurd_check_ids ();
  73.  
  74.   if (!err)
  75.     {
  76.       size_t i, j;
  77.       size_t nu = _hurd_id.gen.nuids, ng = _hurd_id.gen.ngids;
  78.       uid_t newu[nu];
  79.       gid_t newg[ng];
  80.  
  81.       memcpy (newu, _hurd_id.gen.uids, nu * sizeof (uid_t));
  82.       memcpy (newg, _hurd_id.gen.gids, ng * sizeof (gid_t));
  83.  
  84.       for (j = 0; j < nuids; ++j)
  85.     {
  86.       const uid_t uid = uids[j];
  87.       for (i = 0; i < nu; ++i)
  88.         if (newu[i] == uid)
  89.           /* Move the last uid into this slot, and decrease the
  90.          number of uids so the last slot is no longer used.  */
  91.           newu[i] = newu[--nu];
  92.     }
  93.       __vm_deallocate (__mach_task_self (),
  94.                (vm_address_t) uids, nuids * sizeof (uid_t));
  95.  
  96.       for (j = 0; j < ngids; ++j)
  97.     {
  98.       const gid_t gid = gids[j];
  99.       for (i = 0; i < nu; ++i)
  100.         if (newu[i] == gid)
  101.           /* Move the last gid into this slot, and decrease the
  102.          number of gids so the last slot is no longer used.  */
  103.           newu[i] = newu[--nu];
  104.     }
  105.       __vm_deallocate (__mach_task_self (),
  106.                (vm_address_t) gids, ngids * sizeof (gid_t));
  107.  
  108.       err = __USEPORT (AUTH, __auth_makeauth
  109.                (port,
  110.             NULL, 0, MACH_MSG_TYPE_COPY_SEND,
  111.             newu, nu,
  112.             _hurd_id.aux.uids, _hurd_id.aux.nuids,
  113.             newg, ng,
  114.             _hurd_id.aux.uids, _hurd_id.aux.ngids,
  115.             &newauth));
  116.     }
  117.   __mutex_unlock (&_hurd_id.lock);
  118.   HURD_CRITICAL_END;
  119.  
  120.   if (err)
  121.     return err;
  122.  
  123.   err = __setauth (newauth);    /* XXX clobbers errno */
  124.   __mach_port_deallocate (__mach_task_self (), newauth);
  125.   if (err)
  126.     return errno;
  127.  
  128.   return 0;
  129. }
  130.